/**
 * Copyright 2012 Impetus Infotech.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package cn.ilikes.tools.hbase.jpa.indexs.lucenes;

import cn.ilikes.tools.hbase.jpa.indexs.Indexer;
import cn.ilikes.tools.hbase.jpa.metadatas.IEntityMetaData;
import cn.ilikes.tools.untils.PropertyAccessorHelper;

public class LuceneQueryUtils {

	public static String getQuery(String clazzFieldName, String clazzName, String idFieldName, String idFieldValue) {
		StringBuffer sb = new StringBuffer("+");
		sb.append(clazzFieldName);
		sb.append(":");
		sb.append(clazzName);
		sb.append(" AND ");
		sb.append("+");
		sb.append(idFieldName);
		sb.append(":");
		sb.append(idFieldValue);
		return sb.toString();
	}

	public static String getJPAHBaseKeyofEntity(IEntityMetaData metadata, Object entity) {
		Object id = PropertyAccessorHelper.getId(entity, metadata);
		return getJPAHBaseKey(metadata.getEntityClazz(), id);

	}

	public static String getJPAHBaseKeyofId(IEntityMetaData metadata, Object id) {

		return getJPAHBaseKey(metadata.getEntityClazz(), id);

	}

	public static String getJPAHBaseKey(Class<?> clazz, Object id) {

		return clazz.getCanonicalName().toLowerCase() + Indexer.DELIMETER + id;
	}

	/**
	 * Gets the cannonical property name.
	 * 
	 * @param indexName
	 *                  the index name
	 * @param propertyName
	 *                  the property name
	 * 
	 * @return the cannonical property name
	 */
	public static String createLuceneFieldsName(String indexName, String family, String qualifier) {
		return indexName + "." + family + "." + qualifier;
	}

}
